//
//  JWFindBusinessTagVC.m
//  jinwang
//
//  Created by jzb on 2018/4/12.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWFindBusinessTagVC.h"
#import "JWFindBusinessTagCell.h"
#import "JWFindBusinessModel.h"
#import "JWAlertChoseButsView.h"

@interface JWFindBusinessTagVC ()<LSXPopMenuDelegate>
{
    TYAlertController *_alertController;
}
@property (nonatomic,strong) JWFindBusinessTagCell *cell;
@property (nonatomic,strong) NSMutableArray *ListmodelArr;
@property (nonatomic,strong) JWFindBusinessModel *Exitmodel;
@property (nonatomic,assign) int page;

@end

@implementation JWFindBusinessTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self setUpTableView];
    [self makeUrl_Business_businessList];
}

//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWFindBusinessTagCell class])]];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self makeUrl_Business_businessList];
    }];
    //上啦加载更多
    self.tableView.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        _page++;
        [self makeUrl_Business_businessList];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _ListmodelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWFindBusinessTagCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    [self.cell.upDownBtn addTarget:self action:@selector(cellBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.upDownBtn.tag = 1000+indexPath.row;
    JWFindBusinessModel *model = self.ListmodelArr[indexPath.row];
    self.cell.model = model;

    for (UIView *view in self.cell.imgsBGView.subviews) {

        [view removeFromSuperview];
    }
    if (model.imgs.count > 0) {

        self.cell.imgsBGView.imageUrls = model.imgs;
    }
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWFindBusinessModel *model = self.ListmodelArr[indexPath.row];
    
    if (model.imgs.count > 0) {
        
        return 150-17 + [self cellContentViewWith:self.cell.imgsBGView.width withModel:model];
    }else{
        
        return 150;
    }
}
- (CGFloat)cellContentViewWith:(CGFloat)width withModel:(JWFindBusinessModel *)model
{
    CGFloat Height = (width - 20)/3 + 5;
    CGFloat CellHeight = 0;
    
    CGSize size = CGSizeMake(kScreenWidth - 30, MAXFLOAT);//设置高度宽度的最大限度
    CGRect rect = [model.business_title boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    
    if (model.imgs.count % 3 > 0) {
        
        CellHeight = Height*(model.imgs.count/3+1);
        
    }else{
        
        CellHeight = Height*(model.imgs.count/3);
    }
    
    return CellHeight + rect.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    JWBoJianDetailsVC *vc = [JWBoJianDetailsVC new];
//    BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
//    vc.post_id = [NSString stringWithFormat:@"%@",model.post_id];
//    [self.navigationController pushViewController:vc animated:YES];
}
- (void)cellBtnOnclick:(UIButton *)sender{
    
    _Exitmodel = self.ListmodelArr[sender.tag - 1000];
    
    if ([_Exitmodel.user_id integerValue] == [k_user_uid integerValue]) {
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"关闭",@"编辑",@"删除"]  icons:@[@"jw_find_businessCloseIcon",@"jw_find_bojian_editIcon",@"jw_find_bojian_deleteIocn"] menuWidth:100 isShowTriangle:YES delegate:self];
    }else{
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"举报",@"拉黑",@"转发",@"分享"]  icons:@[@"jw_find_bojian_reportIcon",@"jw_find_bojian_blackIcon",@"jw_bj_Forwarding",@"jw_find_businessShearIcon"] menuWidth:120 isShowTriangle:YES delegate:self];
    }
    
}
-(void)LSXPopupMenuDidSelectedAtIndex:(NSInteger)index LSXPopupMenu:(LSXPopMenu *)LSXPopupMenu{
    
    if ([_Exitmodel.user_id integerValue] == [k_user_uid integerValue]) {
        
        if (index == 0) {
            
//            ReleaseBoJianVC *vc = [ReleaseBoJianVC new];
//            vc.model = _Exitmodel;
//            vc.ExitType = @"佳文";
//            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 1){
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                           message:@"是否删除本条博见？"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action) {
                                                                     [self.ListmodelArr removeObject:_Exitmodel];
//                                                                     [self makeUrl_post_delPost];
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }else{
        if (index == 0) {
            //举报
            JWAlertChoseButsView *view = [[JWAlertChoseButsView alloc]initWithFrame:CGRM(0, 0, 750-160, 400)];
            
            view.StrOrArr = @"jubao";
            view.dataList = @[@"广告类",@"诈骗类",@"骚扰类",@"违法类"];
            
            view.layer.cornerRadius = 8.0;
            view.layer.masksToBounds = YES;
            
            view.ProfessionaDateBlcok = ^(NSString *text,NSMutableArray *arr){
                
                if (text.length > 0) {
                    NSString *port_type = @"";
                    if ([text isEqualToString:@"广告类"]) {
                        port_type = @"0";
                    }else if ([text isEqualToString:@"诈骗类"]){
                        port_type = @"1";
                    }else if ([text isEqualToString:@"骚扰类"]){
                        port_type = @"2";
                    }else if ([text isEqualToString:@"违法类"]){
                        port_type = @"3";
                    }
                    [self makeUrl_Business_addReport:port_type];
                }
            };
            
            _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
            _alertController.backgoundTapDismissEnable = YES;
            [self presentViewController:_alertController animated:YES completion:nil];
        }else if (index == 1){
            //拉黑
            [self makeUrl_Business_addBlack];
        }else if (index == 2){
            //只给一窗
//            [self makeUrl_post_addOne];
        }
        else if (index == 3){
            //关注
//            [self makeUrl_post_addFollowFollow_type:@"0"];
        }
    }
}
#pragma mark -网络请求
//商务列表
-(void)makeUrl_Business_businessList{
    [[DDNetworkManagerDate makeUrl_Business_businessListWithtoken:k_user_token tag_id:_tag_id page:[NSString stringWithFormat:@"%d",_page]] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFindBusinessModel *model = [JWFindBusinessModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            //            self.ListmodelArr = arr.mutableCopy;
            if (_page == 1) {
                self.ListmodelArr = arr.mutableCopy;
            }else{
                [self.ListmodelArr addObjectsFromArray:arr];
            }
            
        }
        [self.tableView reloadData];
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//拉黑
-(void)makeUrl_Business_addBlack{
    [[DDNetworkManagerDate makeUrl_Business_addBlackWithtoken:k_user_token user_id:[_Exitmodel.user_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [self makeUrl_Business_businessList];
        }
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}

//举报博见
-(void)makeUrl_Business_addReport:(NSString *)report_type{
    [[DDNetworkManagerDate makeUrl_Business_addReportWithtoken:k_user_token business_id:[_Exitmodel.business_id stringValue] report_type:report_type] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}

-(NSMutableArray *)ListmodelArr{
    if (!_ListmodelArr) {
        _ListmodelArr = [NSMutableArray array];
    }
    return _ListmodelArr;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
